
#include <rtthread.h>
#include "cc1101_drv.h"


#include <rtdevice.h>
#include <drv_gpio.h>
#include "drv_spi.h"
#include <stdio.h>
#include <stdlib.h>



#define DBG_ENABLE
#define DBG_SECTION_NAME "cc1101_drv"
#define DBG_LEVEL        DBG_LOG
#define DBG_COLOR
#include <rtdbg.h>


cc1101_dev_t cc1101_dev_ctx;
#define CC1101_HANDLE &cc1101_dev_ctx
static struct rt_semaphore sem_as07_recWeak;
static uint8_t CC1101_device_addr = 0;
static int16_t cc1101_rssi = 0;
static uint8_t cc1101_Lqi = 0;
static int8_t  cc1101InitState = 0;
static struct rt_spi_device *spi_dev_cc1101;

extern void start_wait_interrupt();
static void stop_wait_interrupt();
extern void rt_as07_weakUp(void *arg);


int16_t CC1101Get1Rssi()
{
	return cc1101_rssi;
}

void stm32_mdelay(uint32_t ms)
{
    __IO uint16_t i;
    for (i = 0; i < ms; i++) {
        rt_hw_us_delay(1000);
    }
}
#define drv_delay_ms stm32_mdelay
#define drv_delay_us rt_hw_us_delay
// 10, 7, 5, 0, -10, -15, -20, -30 dbm output power
const uint8_t PaTabel[] = { 0xc0, 0xC8, 0x84, 0x60, 0x34, 0x1D, 0x0E, 0x12 };


static uint8_t CC1101InitData[][2] = {
    { CC1101_IOCFG2, 0x06 },   // GDO2输出引脚配置
    { CC1101_FIFOTHR, 0x47 },  // RX FIFO和TX FIFO阈值
    { CC1101_PKTCTRL0, 0x05 }, // 分组自动化控制
    { CC1101_CHANNR, 0x0 },    // 信道设置
    { CC1101_FSCTRL1, 0x0C },  // 频率合成器控制
    { CC1101_FREQ2, 0x10 },    // 频率控制字，高字节
    { CC1101_FREQ1, 0xB1 },    // 频率控制字，中字节
    { CC1101_FREQ0, 0x3B },    // 频率控制字，低字节
    { CC1101_MDMCFG4, 0x7B },  // 调制解调器配置
    { CC1101_MDMCFG3, 0x83 },  // 调制解调器配置
    { CC1101_MDMCFG2, 0x13 },  // 调制解调器配置
    { CC1101_DEVIATN, 0x42 },  // 调制解调器偏差设置
    { CC1101_MCSM1, 0x3B },    // 主无线电控制状态机配置
    { CC1101_MCSM0, 0x18 },    // 主无线电控制状态机配置
    { CC1101_FOCCFG, 0x1D },   // 频偏补偿配置
    { CC1101_WORCTRL, 0xFB },  // 无线电唤醒控制
    { CC1101_FSCAL3, 0xEA },   // 频率合成器校准
    { CC1101_FSCAL2, 0x2A },   // 频率合成器校准
    { CC1101_FSCAL1, 0x00 },   // 频率合成器校准
    { CC1101_FSCAL0, 0x1F },   // 频率合成器校准
    { CC1101_TEST2, 0x81 },    // 各种测试设置
    { CC1101_TEST1, 0x35 },    // 各种测试设置
    { CC1101_FREND0, 0x10 }    // PA设置
};
uint8_t InitDataRead[sizeof(CC1101InitData) / 2];
extern void CC1101_Clear_TxBuffer(cc1101_dev_t *dev);
extern void CC1101_Clear_RxBuffer(cc1101_dev_t *dev);
static int write_cmd(void *handle, uint8_t cmd)
{
    struct rt_spi_device *spiDevice = (struct rt_spi_device *)handle;
    rt_size_t len = rt_spi_send(spiDevice, &cmd, 1);
    if (len != 1) {
        LOG_E("c1101_write_cmd error. %d", len);
        return -RT_ERROR;
    }
    return RT_EOK;
}
static int write_reg(void *handle, uint8_t *bufp, uint16_t len)
{
    struct rt_spi_device *spiDevice = (struct rt_spi_device *)handle;
    rt_size_t len_r = rt_spi_send(spiDevice, bufp, len);
    if (len_r != len) {
        LOG_E("c1101_write_reg error. %d", len_r);
        return -RT_ERROR;
    }
    return RT_EOK;
}
static int read_reg(void *handle, const void *send_buf, rt_size_t send_length,
                    void *recv_buf, rt_size_t recv_length)
{
    struct rt_spi_device *spiDevice = (struct rt_spi_device *)handle;
    rt_size_t len_r = rt_spi_send_then_recv(spiDevice, send_buf, send_length,
                                            recv_buf, recv_length);
    if (len_r != RT_EOK) {
        LOG_E("c1101_read_reg error. %d", len_r);
        return -RT_ERROR;
    }
    return RT_EOK;
}

int32_t read_multi_reg(void *handle, uint8_t addr, void *recv_buf,
                       rt_size_t recv_length)
{
    struct rt_spi_device *spiDevice = (struct rt_spi_device *)handle;
    rt_size_t result =
        rt_spi_send_then_recv(spiDevice, &addr, 1, recv_buf, recv_length);
    if (result != RT_EOK) {
        LOG_E("c1101_read_multi_reg error. %d", result);
        return -RT_ERROR;
    }
    return RT_EOK;
}
uint32_t CC1101_GET_GDO0_STATUS(cc1101_dev_t *dev)
{
    return rt_pin_read(dev->gdo0_pin);
}
void CC1101_Write_Cmd(cc1101_dev_t *dev, uint8_t Command)
{
    dev->write_cmd(dev->handle, Command);
}
void CC1101_Write_Reg(cc1101_dev_t *dev, uint8_t Addr, uint8_t WriteValue)
{
    uint8_t buff[2];
    buff[0] = Addr;
    buff[1] = WriteValue;
    dev->write_reg(dev->handle, buff, 2);
}
static uint8_t spiWirtebuff[256] = { 0 };
void CC1101_Write_Multi_Reg(cc1101_dev_t *dev, uint8_t addr,
                            uint8_t *pWriteBuff, uint8_t writeSize)
{
    if (writeSize == 0 || writeSize > 254) {
        return;
    }
    spiWirtebuff[0] = addr | WRITE_BURST;
    memcpy(&spiWirtebuff[1], pWriteBuff, writeSize);
    dev->write_reg(dev->handle, spiWirtebuff, writeSize + 1);
}
uint8_t CC1101_Read_Reg(cc1101_dev_t *dev, uint8_t Addr)
{
    uint8_t writeBuff = Addr | READ_SINGLE;
    uint8_t readBuff = 0xFF;
    if (dev->read_reg(dev->handle, &writeBuff, 1, &readBuff, 1) != RT_EOK) {
        LOG_E("Failed to read register 0x%02X", Addr);
        return 0xFF; // 返回默认值以表示读取失败
    }
    return readBuff; // 返回读取的寄存器值
}
uint8_t CC1101_Read_Status(cc1101_dev_t *dev, uint8_t Addr)
{
    uint8_t readBuff[2] = { 0xff, 0xff };
    uint8_t writeBuff[2];
    writeBuff[0] = Addr | READ_BURST;
    dev->read_reg(dev->handle, writeBuff, 1, readBuff, 1);
    return readBuff[0];
}
void CC1101_Read_Multi_Reg(cc1101_dev_t *dev, uint8_t Addr, uint8_t *pReadBuff,
                           uint8_t ReadSize)
{
    uint8_t writeBuff[1];

    writeBuff[0] = Addr | READ_BURST;

    dev->read_multi_reg(dev->handle, writeBuff[0], pReadBuff, ReadSize);
}
uint8_t cur_SetMode;
void CC1101_Set_Mode(cc1101_dev_t *dev, CC1101_ModeType Mode)
{
    uint8_t i = 0;
    if (Mode == TX_MODE) // 发送模式
    {
		
        CC1101_Write_Reg(dev, CC1101_IOCFG2, 0x06);
        CC1101_Write_Cmd(dev, CC1101_STX);

    } else if (Mode == RX_MODE) // 接收模式
    {
        CC1101_Write_Reg(dev, CC1101_IOCFG2, 0x06);
        CC1101_Write_Cmd(dev, CC1101_SRX);
    }
}
void CC1101_Set_Idle_Mode(cc1101_dev_t *dev)
{
    CC1101_Write_Cmd(dev, CC1101_SIDLE);
}

void CC1101_start_WOR(cc1101_dev_t *dev)
{
    rt_pin_mode(cc1101_dev_ctx.irq_pin, PIN_MODE_INPUT_PULLUP);
    rt_pin_attach_irq(cc1101_dev_ctx.irq_pin, PIN_IRQ_MODE_FALLING,
                      rt_as07_weakUp, RT_NULL);
    rt_pin_irq_enable(cc1101_dev_ctx.irq_pin, PIN_IRQ_ENABLE);
    CC1101_Write_Reg(dev, CC1101_IOCFG2, 0x06);
    CC1101_Write_Cmd(dev, CC1101_SWOR);
}
void C1101_WOR_Init(cc1101_dev_t *dev)
{
    // uint16_t envtTimeout=0x8cA0;
    uint16_t envtTimeout = 0xccA0;
    CC1101_Write_Reg(dev, CC1101_MCSM0, 0x18);
    CC1101_Write_Reg(dev, CC1101_WORCTRL, 0x78);
    CC1101_Write_Reg(dev, CC1101_MCSM2, 0x00);
    CC1101_Write_Reg(dev, CC1101_WOREVT1, envtTimeout >> 8);
    CC1101_Write_Reg(dev, CC1101_WOREVT0, envtTimeout & 0xff);
    CC1101_Write_Cmd(dev, CC1101_SWORRST); // 写入WOR命令
}
/**
 * @brief :CC1101设置地址
 * @param :
 *			@Address：设置的设备地址值
 *			@AddressMode：地址检测模式
 * @note  :无
 * @retval:无
 */
uint8_t CC1101_Read_Reg_test;
void CC1101_Set_Address(cc1101_dev_t *dev, uint8_t Address,
                        CC1101_AddrModeType AddressMode)
{
    uint8_t btmp = 0;
    btmp = CC1101_Read_Reg(dev, CC1101_PKTCTRL1) &
           ~0x03; // 读取CC1101_PKTCTRL1寄存器初始值
    CC1101_Write_Reg(dev, CC1101_ADDR, Address); // 设置设备地址
    if (AddressMode == BROAD_ALL) {
    } // 不检测地址
    else if (AddressMode == BROAD_NO) {
        btmp |= 0x01; // 检测地址 但是不带广播
    } else if (AddressMode == BROAD_0) {
        btmp |= 0x02; // 0x00为广播
    } else if (AddressMode == BROAD_0AND255) {
        btmp |= 0x03; // 0x00 0xFF为广播
    }
    CC1101_device_addr = Address;
    CC1101_Read_Reg_test = CC1101_Read_Reg(dev, CC1101_ADDR);
    CC1101_Write_Reg(dev, CC1101_PKTCTRL1, btmp); // 写入地址模式
    // CC1101_Read_Reg_test = CC1101_Read_Reg(dev, CC1101_PKTCTRL1);
}

/**
 * @brief :CC1101设置同步字段
 * @param :无
 * @note  :无
 * @retval:无
 */
void CC1101_Set_Sync(cc1101_dev_t *dev, uint16_t Sync)
{
    CC1101_Write_Reg(dev, CC1101_SYNC1, 0xFF & (Sync >> 8));
    CC1101_Write_Reg(dev, CC1101_SYNC0, 0xFF & Sync); // 写入同步字段 16Bit
}

/**
 * @brief :下电模式
 * @param :无
 * @note  :无
 * @retval:无
 */
void CC1101_Set_Power_Down(cc1101_dev_t *dev)
{
    CC1101_Write_Cmd(dev, CC1101_SPWD);
}
int16_t CalculateRssi(uint8_t RSSI_dec)
{
    int16_t temp;
    if (RSSI_dec >= 128) {
        temp = (int16_t)((int16_t)(RSSI_dec - 256) / 2) - RSSI_offset;
    } else {
        temp = (RSSI_dec / 2) - RSSI_offset;
    } /**/
    return temp;
}

/*
 * @brief :CC1101清空发送缓冲区
 * @param :无
 * @note  :无
 * @retval:无
 */
void CC1101_Clear_TxBuffer(cc1101_dev_t *dev)
{
    CC1101_Set_Idle_Mode(dev);          // 首先进入IDLE模式
    CC1101_Write_Cmd(dev, CC1101_SFTX); // 写入清发送缓冲区命令
}

/**
 * @brief :CC1101清空接收缓冲区
 * @param :无
 * @note  :无
 * @retval:无
 */
void CC1101_Clear_RxBuffer(cc1101_dev_t *dev)
{
    CC1101_Set_Idle_Mode(dev);          // 首先进入IDLE模式
    CC1101_Write_Cmd(dev, CC1101_SFRX); // 写入清接收缓冲区命令
}
void clear_cc1101_buff(uint8_t *buff, uint16_t len)
{
    memset(buff, '\0', len);
}

/**
 * @brief :CC1101发送数据包
 * @param :
 *			@pTxBuff：发送数据缓冲区
 *			@TxSize：发送数据长度
 *			@DataMode：数据模式
 * @note  :无
 * @retval:无
 */
void CC1101_Tx_Packet(cc1101_dev_t *dev, uint8_t *pTxBuff, uint8_t TxSize,
                      CC1101_TxDataModeType DataMode)
{
    uint8_t Address;
    uint16_t l_RxWaitTimeout = 0;

    if (DataMode == BROADCAST) {
        Address = 0;
    } else if (DataMode == ADDRESS_CHECK) {
        Address = CC1101_device_addr;
    }

    CC1101_Clear_TxBuffer(dev);

    if ((CC1101_Read_Reg(dev, CC1101_PKTCTRL1) & 0x03) != 0) {
        CC1101_Write_Reg(dev, CC1101_TXFIFO, TxSize + 1);
        CC1101_Write_Reg(
            dev, CC1101_TXFIFO,
            Address); // 写入长度和地址 由于多一个字节地址此时长度应该加1
    } else {
        CC1101_Write_Reg(dev, CC1101_TXFIFO, TxSize); // 只写长度 不带地址
    }

    CC1101_Write_Multi_Reg(dev, CC1101_TXFIFO, pTxBuff, TxSize); // 写入数据
    CC1101_Set_Mode(dev, TX_MODE);                               // 发送模式
}

void CC1101_Tx_Packet_Addr(cc1101_dev_t *dev, uint8_t *pTxBuff, uint8_t TxSize,
                           uint8_t addr, CC1101_TxDataModeType DataMode)
{
    uint8_t Address;
    uint16_t l_RxWaitTimeout = 0;

    if (DataMode == BROADCAST) {
        Address = 0;
    } else if (DataMode == ADDRESS_CHECK) {
        Address = addr;
    }

    CC1101_Clear_TxBuffer(dev);

    if ((CC1101_Read_Reg(dev, CC1101_PKTCTRL1) & 0x03) != 0) {
        CC1101_Write_Reg(dev, CC1101_TXFIFO, TxSize + 1);
        CC1101_Write_Reg(
            dev, CC1101_TXFIFO,
            Address); // 写入长度和地址 由于多一个字节地址此时长度应该加1
    } else {
        CC1101_Write_Reg(dev, CC1101_TXFIFO, TxSize); // 只写长度 不带地址
    }

    CC1101_Write_Multi_Reg(dev, CC1101_TXFIFO, pTxBuff, TxSize); // 写入数据
    CC1101_Set_Mode(dev, TX_MODE);                               // 发送模式
}
/**
 * @brief :CC1101读取接收到的字节数
 * @param :无
 * @note  :无
 * @retval:接收到的数据个数
 */
uint8_t CC1101_Get_RxCounter(cc1101_dev_t *dev)
{
    return (CC1101_Read_Status(dev, CC1101_RXBYTES) & BYTES_IN_RXFIFO);
}

/**
 * @brief :CC1101接收数据包
 * @param :
 *			@RxBuff：发送数据缓冲区
 * @note  :无
 * @retval：接收到的字节数，0表示无数据
 */
uint8_t CC1101_Rx_Packet(cc1101_dev_t *dev, uint8_t *RxBuff)
{
    uint8_t l_PktLen = 0;
    uint8_t l_Status[2] = { 0 };
    uint16_t l_RxWaitTimeout = 0;

    while (1 == CC1101_GET_GDO0_STATUS(dev)) // 等待接收完成
    {
        drv_delay_ms(1);
        if (100 == l_RxWaitTimeout++) {
            l_RxWaitTimeout = 0;
            break;
        }
    }
    if (0 != CC1101_Get_RxCounter(dev)) {
        l_PktLen = CC1101_Read_Reg(dev, CC1101_RXFIFO); // 获取长度信息
        if ((CC1101_Read_Reg(dev, CC1101_PKTCTRL1) & 0x03) != 0) {
            CC1101_Read_Reg(
                dev,
                CC1101_RXFIFO); // 如果数据包中包含地址信息 ，则读取地址信息
        }
        if (l_PktLen == 0) {
            return 0; // 无数据
        } else {
            l_PktLen--; // 减去一个地址字节
        }
        CC1101_Read_Multi_Reg(dev, CC1101_RXFIFO, RxBuff,
                              l_PktLen); // 读取数据
        CC1101_Read_Multi_Reg(
            dev, CC1101_RXFIFO, l_Status,
            2); // 读取数据包最后两个额外字节，后一个为CRC标志位

        CC1101_Clear_RxBuffer(dev);
        cc1101_rssi = CalculateRssi(l_Status[0]);
        cc1101_Lqi = l_Status[0] & 0x7f;
        if (l_Status[1] & CRC_OK) {
            return l_PktLen;
        } else {
            return 0;
        }
    } else {
        return 0;
    }
}

/**
 * @brief :CC1101复位
 * @param :无
 * @note  :无
 * @retval:无
 */
uint8_t cc1101_ver;
void CC1101_Reset(cc1101_dev_t *dev)
{
    rt_pin_write(dev->ce_pin, PIN_HIGH);
    drv_delay_us(3000);
    rt_pin_write(dev->ce_pin, PIN_LOW);
    drv_delay_us(4000);
    rt_pin_write(dev->ce_pin, PIN_HIGH);
    drv_delay_us(3000);
    CC1101_Write_Cmd(dev, CC1101_SRES);
    CC1101_Read_Reg_test = CC1101_Read_Reg(dev, CC1101_ADDR);

    drv_delay_us(3000);
	CC1101_Write_Reg(dev, CC1101_TEST2,0x78);
    cc1101_ver = CC1101_Read_Reg(dev, CC1101_TEST2);
    if (cc1101_ver != 0x78) {
        cc1101InitState = RT_ERROR;
        LOG_E("g_sysDecive_state.cc1101_state = DEVICE_SATE_ERROR;");
    } else {
        cc1101InitState = RT_EOK;
		LOG_E("cc1101 version  = %d;",cc1101_ver);
    }


}

// 这里低功耗的话, 要记得 把CE管脚关掉

static int rt_as07_cc1101_work_init(cc1101_dev_t *dev)
{
    GPIO_InitTypeDef GPIO_InitStruct = { 0 };
    __HAL_RCC_SPI1_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();
    GPIO_InitStruct.Pin = GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    rt_pin_mode(dev->ce_pin, PIN_MODE_OUTPUT);
    rt_pin_mode(dev->power_pin, PIN_MODE_OUTPUT);

    rt_pin_write(dev->ce_pin, PIN_HIGH);
    rt_pin_write(dev->power_pin, PIN_HIGH);

    rt_pin_write(dev->ce_pin, PIN_HIGH);
    rt_pin_write(dev->power_pin, PIN_HIGH);
    rt_pin_mode(dev->irq_pin, PIN_MODE_INPUT_PULLUP);
    rt_pin_attach_irq(dev->irq_pin, PIN_IRQ_MODE_FALLING, rt_as07_weakUp,
                      RT_NULL);
    rt_pin_irq_enable(dev->irq_pin, PIN_IRQ_ENABLE);
    return 0;
}

 int CC1101_power_off()
{
		cc1101_dev_t *dev =CC1101_HANDLE;
    GPIO_InitTypeDef GPIO_InitStruct = { 0 };
    __HAL_RCC_SPI1_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();
    GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 |
                          GPIO_PIN_6 | GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

		
//		GPIO_InitStruct.Pin = GPIO_PIN_3;           // 替换为目标引脚
//    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; // 设置为推挽输出
//    GPIO_InitStruct.Pull = GPIO_PULLDOWN;         // 无上下拉（根据需要可改为 PULLUP 或 PULLDOWN）
//    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; // 设置为低速（低功耗）
//    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
//    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET);
		
    rt_pin_write(dev->ce_pin, PIN_LOW);
    rt_pin_write(dev->power_pin, PIN_LOW);
    return 0;
}

void rt_as07_spi_init(void)
{
    struct rt_spi_configuration cfg;
    spi_dev_cc1101 = (struct rt_spi_device *)rt_device_find("as07");
    /* config spi */
    if (spi_dev_cc1101) {
        cfg.data_width = 8;
        cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB;
        cfg.max_hz = 1 * 1000 * 1000; /* 42M,SPI max 42MHz,lcd 4-wire spi */

        rt_spi_configure(spi_dev_cc1101, &cfg);
    }
}

uint8_t readBuffTemp[10] = { 0xff };
int8_t CC1101_Init(uint16_t sync_word)
{
    uint8_t i = 0;
    stop_wait_interrupt();
    rt_as07_cc1101_work_init(CC1101_HANDLE);
    CC1101_Reset(CC1101_HANDLE);
    for (i = 0; i < (sizeof(CC1101InitData) / 2); i++) {
        CC1101_Write_Reg(CC1101_HANDLE, CC1101InitData[i][0],
                         CC1101InitData[i][1]); // 写入配置参数
        InitDataRead[i] = CC1101_Read_Reg(CC1101_HANDLE, CC1101InitData[i][0]);
        // LOG_I("%d:%x", i, InitDataRead[i]);
    }

    CC1101_Set_Address(CC1101_HANDLE, 5, BROAD_0AND255);
    CC1101_Set_Sync(CC1101_HANDLE, sync_word); // 写入同步字段
    CC1101_Write_Reg(CC1101_HANDLE, CC1101_MDMCFG1,
                     0x72); // 调制解调器配置

    CC1101_Write_Multi_Reg(CC1101_HANDLE, CC1101_PATABLE, (uint8_t *)PaTabel, 8);
    start_wait_interrupt();
	CC1101_Clear_RxBuffer(CC1101_HANDLE);
	CC1101_Clear_TxBuffer(CC1101_HANDLE);
	return cc1101InitState;
}

void CC1101_Init_Param(cc1101_dev_t *dev)
{
    CC1101_Write_Multi_Reg(dev, CC1101_PATABLE, (uint8_t *)PaTabel, 8);
}

int rt_as07_cc1101_init(void)
{
    cc1101_dev_ctx.read_reg = read_reg;
    cc1101_dev_ctx.write_cmd = write_cmd;
    cc1101_dev_ctx.write_reg = write_reg;
    cc1101_dev_ctx.read_multi_reg = read_multi_reg;

    cc1101_dev_ctx.gdo0_pin = GET_PIN(A, 0);

    cc1101_dev_ctx.ce_pin = GET_PIN(A, 4);

    cc1101_dev_ctx.power_pin = GET_PIN(A, 3);

    cc1101_dev_ctx.irq_pin = GET_PIN(A, 0);
    rt_hw_spi_device_attach("spi1", "as07", cc1101_dev_ctx.ce_pin);

    rt_as07_spi_init();
    cc1101_dev_ctx.handle = spi_dev_cc1101;
	rt_sem_init(&sem_as07_recWeak, "as07_rec", 0, RT_IPC_FLAG_FIFO);
    LOG_I("rt_as07_cc1101_init_Handle_");
	return 0;
}

uint8_t inputter_read = 0;
void rt_as07_weakUp(void *arg)
{
    if (inputter_read) {
        rt_sem_release(&sem_as07_recWeak);
        // LOG_X("asm07_recive new message");
    }
}
static void start_wait_interrupt()
{
    sem_as07_recWeak.value = 0;
    inputter_read = 1;
}
static void stop_wait_interrupt()
{
    inputter_read = 0;
}
int CC1101_SendPacket(uint8_t *da, uint8_t len)
{
    int result = 0;
	// CC1101_Clear_TxBuffer(CC1101_HANDLE);
	// CC1101_Set_Mode(CC1101_HANDLE, TX_MODE);
	
    CC1101_Tx_Packet(CC1101_HANDLE, da, len, BROADCAST);

    if (rt_sem_take(&sem_as07_recWeak, 5000) == RT_EOK) {
        result = RT_TRUE;
    } else {
        LOG_E("send data error ");
        result = RT_FALSE;
    }
    return result;
}
int CC1101_WaitPack(uint8_t *da, uint32_t wait_time)
{
    uint16_t rec_count;
		CC1101_Clear_RxBuffer(CC1101_HANDLE);
    CC1101_Set_Mode(CC1101_HANDLE, RX_MODE);
    if (rt_sem_take(&sem_as07_recWeak, wait_time) == RT_EOK) {
        rec_count = CC1101_Rx_Packet(CC1101_HANDLE,
                                     da); // 接收字节
        if (rec_count > 0) {
            LOG_I("asm07_recive %d  , RSSI:%d  quid:%d", rec_count, cc1101_rssi,
                  cc1101_Lqi);

        } else {
            LOG_I("message error");
        }
    } else {
        LOG_I("no message ");
        rec_count = 0;
    }
    CC1101_Clear_RxBuffer(CC1101_HANDLE);
    return rec_count;
}